package PlotPourri.Database;

import PlotPourri.BandName;
import PlotPourri.Observation;
import PlotPourri.Star;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DatabaseLMCi extends StarDatabase {

    public DatabaseLMCi(File path) {
        super(path);
        Scanner sc = null;
        try {
            sc = new Scanner(path);
        } catch (FileNotFoundException ex) {
            System.err.print(ex + "\n");
        }
        if (sc != null) {
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                if (line.startsWith("#")) continue;
                Star star;
                int name = Integer.parseInt(line.split("\\s+")[0].split("-")[3]);
                if (containsKey(name)) star = get(name); // stars.contains etc
                else star = new Star(BandName.getVI(), name);
                //star.setPeriod(Math.pow(10, Double.parseDouble(line.split("\\s+")[1])));
                star.setPeriod(Double.parseDouble(line.split("\\s+")[1]));
                for (BandName bandName : BandName.getVI()) {
                    String full = path.getPath().split("OgleIII-1O.dat")[0] + bandName + File.separatorChar + line.split("\\s+")[0] + ".dat";
                    Observation datfile = new Observation(full, Integer.parseInt(full.split("-")[full.split("-").length - 1].split(".dat")[0]));
                    datfile.setPhasePosition(-1);
                    datfile.setMagnitudePosition(1);
                    datfile.setErrorPosition(2);
                    datfile.setJulianDatePosition(0);
                    datfile.setPeriod(star.getPeriod());
                    //double Av = Double.parseDouble(line.split("\\s+")[4]);
                    //if (Av == 0) Av = 0.324;
                    //if (bandName.equals(BandName.V)) datfile.setReddening(Av);
                    //else datfile.setReddening((1.96 / 3.24) * Av);
                    star.getBand(bandName).add(datfile);
                }
                if (!containsKey(name)) put(star.getName(), star); // stars.contains etc
            }
            sc.close();
        }
    }
}
